package com.bwie.home.backstage.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.home.backstage.entity.FitmentEntity;
import com.bwie.home.backstage.entity.MarketEntity;
import com.bwie.home.backstage.entity.vo.MyHouses;
import com.bwie.home.backstage.service.FitmentService;
import com.bwie.home.backstage.service.MarketService;
import com.bwie.home.common.utils.OosSave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bwie.home.backstage.entity.HousesEntity;
import com.bwie.home.backstage.service.HousesService;
import com.bwie.home.common.utils.PageUtils;
import com.bwie.home.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author sy
 * @email sunlightcs@gmail.com
 * @date 2021-09-10 09:46:41
 */
@RestController
@RequestMapping("backstage/houses")
public class HousesController {
    @Autowired
    private HousesService housesService;

    @Autowired
    private OosSave oosSave;

    //查询楼盘链表
    @RequestMapping("/HousesList")
    public R HousesList(@RequestBody Map<String, Object> params){
        PageUtils page = housesService.HousesList(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@RequestBody HousesEntity housesEntity){
        PageUtils page = housesService.queryPage(params,housesEntity);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{housesId}")
    public R info(@PathVariable("housesId") Integer housesId){
		HousesEntity houses = housesService.getById(housesId);

        return R.ok().put("houses", houses);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HousesEntity houses){
		housesService.save(houses);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HousesEntity houses){
		housesService.updateById(houses);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] housesIds){
		housesService.removeByIds(Arrays.asList(housesIds));

        return R.ok();
    }

    @RequestMapping("/DeleteHousesid/{housesId}")
    public R DeleteHousesid(@PathVariable Integer housesId){
        housesService.removeById(housesId);

        return R.ok();
    }

    @PostMapping("/img")
    public R img(MultipartFile file){
        String s = oosSave.saveObject(file.getOriginalFilename(), file);
        return R.ok().put("img",s);
    }

    @Autowired
    private FitmentService fitmentService;
    @Autowired
    private MarketService marketService;
    @RequestMapping("/addHouses")
    public R addHouses(@RequestBody MyHouses myHouses)throws Exception {
        HousesEntity housesEntity = new HousesEntity();
        housesEntity.setHousesDistrict(myHouses.getProvinc() +"-"+ myHouses.getCity()+"-" + myHouses.getCounty());
        housesEntity.setHousesProvince(myHouses.getProvinc());
        housesEntity.setHousesCity(myHouses.getCity());
        housesEntity.setHousesCounty(myHouses.getCounty());
        housesEntity.setHousesName(myHouses.getHousesName());
        housesEntity.setHousesSite(myHouses.getHousesSite());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date OpeningTime = format.parse(myHouses.getHousesOpeningTime());
        housesEntity.setHousesOpeningTime(OpeningTime);
        housesEntity.setHousesPhone(myHouses.getHousesPhone());
        housesEntity.setDevelopersName(myHouses.getDevelopersName());
        housesEntity.setHousesRedpacket(new BigDecimal(myHouses.getHousesRedpacket()));
        housesEntity.setHousesPrice(myHouses.getHousesPrice() + myHouses.getHousesPricearea());
        housesEntity.setDevelopersSite(myHouses.getDevelopersSite());
        Date CheckTime = format.parse(myHouses.getHousesCheckTime());
        housesEntity.setHousesCheckTime(CheckTime);
        housesEntity.setDevelopersCertificate(myHouses.getDevelopersCertificate());
        housesEntity.setDevelopersCoordinate(myHouses.getDevelopersCoordinate());
        housesEntity.setHousesDiscounts(myHouses.getChecked() ? 1 : 0);
        FitmentEntity fitmentName = fitmentService.getOne(new QueryWrapper<FitmentEntity>().
                eq("fitment_name", myHouses.getFitmentId()));
        housesEntity.setFitmentId(fitmentName.getFitmentId());
        MarketEntity market = marketService.getOne(new QueryWrapper<MarketEntity>().
                eq("market_name", myHouses.getMarketId()));
        housesEntity.setMarketId(market.getMarketId());
        String[] typeId = myHouses.getTypeId();
        String type = "";
        for (String type1 : typeId) {
            type = "," + type1;
        }
        type = type.substring(1);
        housesEntity.setTypeId(type);
        String[] fea = myHouses.getFeatureId();
        String feaid = "";
        for (String feat : fea) {
            feaid = "," + feat;
        }
        feaid = feaid.substring(1);
        housesEntity.setFeatureId(feaid);
        String[] recommend = myHouses.getRecommendId();
        String rec = "";
        for (String red : recommend) {
            rec = "," + red;
        }
        rec = rec.substring(1);
        housesEntity.setRecommendId(rec);
        housesEntity.setHousesImg(myHouses.getImg());
        housesEntity.setHousesVideo(myHouses.getHousesVideo());
        housesEntity.setHousesIntroduce(myHouses.getHousesIntroduce());
        housesEntity.setHousesArchitecture(myHouses.getHousesArchitecture());
        housesEntity.setHousesAge(Integer.parseInt(myHouses.getHousesAge()));
        housesEntity.setHousesArea(myHouses.getHousesArea());
        housesEntity.setHousesArchitectureArea(myHouses.getHousesArchitectureArea());
        housesEntity.setHousesPlot(myHouses.getHousesPlot());
        housesEntity.setHousesAfforest(myHouses.getHousesAfforest());
        housesEntity.setHousesProject(myHouses.getHousesProject());
        housesEntity.setHousesStall(myHouses.getHousesStall());
        housesEntity.setHousesReal(myHouses.getHousesReal());
        housesEntity.setHousesProperty(myHouses.getHousesProperty());
        housesEntity.setHousesPropertyfee(myHouses.getHousesPropertyfee());
        housesEntity.setPanoramaSite(myHouses.getPanoramaSite());

        housesEntity.setHousesSort(0);
        housesEntity.setHousesTime(new Date());
        boolean save = housesService.save(housesEntity);
        if (save){
            return R.ok();
        }
        return R.error();

    }

}
